const Controller = require('egg').Controller;

class CirclePostCommentController extends Controller {
    // 创建评论
    async create() {
        const { ctx, service } = this;

        try {
            // 参数验证
            const rules = {
                circle_post_id: { type: 'number', required: true },
                content: { type: 'string', required: true },
                parent_id: { type: 'integer', required: false }
            };
            ctx.validate(rules);

            const commentData = ctx.request.body;

            // 从token中获取用户ID
            const token = ctx.request.header.token;
            const decode = ctx.app.jwt.verify(token, ctx.app.config.jwt.secret);

            // 检查用户是否加入了该圈子
            await service.circlePostComment.checkUserInCircle(decode.user_id, commentData.circle_post_id);

            const result = await service.circlePostComment.create({
                ...commentData,
                user_id: decode.user_id
            });

            ctx.body = {
                code: 0,
                data: result,
                msg: '评论成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '评论失败'
            };
        }
    }

    // 获取评论列表
    async list() {
        const { ctx, service } = this;

        try {
            const { circle_post_id } = ctx.query;

            // 验证参数
            const rules = {
                circle_post_id: { type: 'id', required: true }
            };
            ctx.validate(rules, ctx.query);

            const result = await service.circlePostComment.getComments(circle_post_id, ctx.query);

            ctx.body = {
                code: 0,
                data: result,
                msg: '获取成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '获取评论列表失败'
            };
        }
    }

    // 获取评论详情
    async detail() {
        const { ctx, service } = this;

        try {
            const { id } = ctx.params;

            // 验证ID参数
            const rules = {
                id: { type: 'id', required: true }
            };
            ctx.validate(rules, ctx.params);

            const comment = await service.circlePostComment.getCommentById(id);

            ctx.body = {
                code: 0,
                data: comment,
                msg: '获取成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '获取评论详情失败'
            };
        }
    }

    // 更新评论
    async update() {
        const { ctx, service } = this;

        try {
            const { id } = ctx.params;
            const updateData = ctx.request.body;

            // 验证路径参数
            const paramRules = {
                id: { type: 'id', required: true }
            };
            ctx.validate(paramRules, ctx.params);

            // 验证body参数
            const bodyRules = {
                content: { type: 'string', required: true }
            };
            ctx.validate(bodyRules);

            // 从token中获取用户ID
            const token = ctx.request.header.token;
            const decode = ctx.app.jwt.verify(token, ctx.app.config.jwt.secret);

            const result = await service.circlePostComment.updateComment(id, updateData, decode.user_id);

            ctx.body = {
                code: 0,
                data: result,
                msg: '更新成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '更新评论失败'
            };
        }
    }

    // 删除评论
    async delete() {
        const { ctx, service } = this;

        try {
            const { id } = ctx.params;

            // 验证ID参数
            const rules = {
                id: { type: 'id', required: true }
            };
            ctx.validate(rules, ctx.params);

            // 从token中获取用户ID
            const token = ctx.request.header.token;
            const decode = ctx.app.jwt.verify(token, ctx.app.config.jwt.secret);

            await service.circlePostComment.deleteComment(id, decode.user_id);

            ctx.body = {
                code: 0,
                data: null,
                msg: '删除成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '删除评论失败'
            };
        }
    }

    // 获取评论回复列表
    async replies() {
        const { ctx, service } = this;

        try {
            const { comment_id } = ctx.query;

            // 验证参数
            const rules = {
                comment_id: { type: 'id', required: true }
            };
            ctx.validate(rules, ctx.query);

            const result = await service.circlePostComment.getReplies(comment_id, ctx.query);

            ctx.body = {
                code: 0,
                data: result,
                msg: '获取成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '获取回复列表失败'
            };
        }
    }

}

module.exports = CirclePostCommentController;